package com.example.resourceSharingPlatform.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.example.resourceSharingPlatform.dao.AccountDao;
import com.example.resourceSharingPlatform.dao.ItemDao;
import com.example.resourceSharingPlatform.dao.UserDao;
import com.example.resourceSharingPlatform.entity.Account;
import com.example.resourceSharingPlatform.entity.Item;
import com.example.resourceSharingPlatform.entity.SubTransaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.resourceSharingPlatform.dao.TransactionDao;
import com.example.resourceSharingPlatform.entity.Transaction;

@Service
public class TransactionService {
	
	@Autowired
    private TransactionDao transactionDao;
	@Autowired
    private ItemDao itemDao;
	@Autowired
    private UserDao userDao;
	@Autowired
    private AccountDao accountDao;
	
	/*查询
	 * 1.根据id查询
	 * 2.根据lender_id查询
	 * 3.根据borrow_id查询
	 * 4.根据item_id查询
	 */
    public Transaction queryTransactionById(int id){
        return transactionDao.queryTransactionById(id);
    }
    
    public List<Transaction> queryTransactionByLender_id(String lender_id){
        List<Transaction> transactionList = transactionDao.queryTransactionByLender_id(lender_id);
        List<Transaction> subTransList = new ArrayList<>();
        for(Transaction t : transactionList){
            SubTransaction subT = new SubTransaction(t);
            subT.setItemName(itemDao.queryItemById(t.getItem_id()).getName());
            subT.setLenderTel(userDao.queryUserById(t.getLender_id()).getTel());
            subT.setBorrowerTel(userDao.queryUserById(t.getBorrow_id()).getTel());
            subTransList.add(subT);
        }

    	return subTransList;
    }
    
    public List<Transaction> queryTransactionByBorrow_id(String borrow_id){
        List<Transaction> transactionList = transactionDao.queryTransactionByLender_id(borrow_id);
        List<Transaction> subTransList = new ArrayList<>();
        for(Transaction t : transactionList){
            SubTransaction subT = new SubTransaction(t);
            subT.setItemName(itemDao.queryItemById(t.getItem_id()).getName());
            subT.setLenderTel(userDao.queryUserById(t.getLender_id()).getTel());
            subT.setBorrowerTel(userDao.queryUserById(t.getBorrow_id()).getTel());
            subTransList.add(subT);
        }

        return subTransList;
    }
    
    public List<Transaction>  queryTransactionByItem_id(int item_id){
    	return transactionDao.queryTransactionByItem_id(item_id);
    }
    
    /*
     * 申请交易插入语句
     */
    public Transaction insertTransaction(Transaction transaction){
        //设置时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        transaction.setTime(dateFormat.format(date));
        //设置其他属性
        transaction.setState("0");//待审核

        return transactionDao.insertTransaction(transaction);
    }

    /**
     * 更新交易状态
     * -1代表交易被拒绝，0代表交易待确认，1代表交易进行中， 2代表交易已完成
     * */
    public synchronized Boolean updateTransState(int id, String state){
        Transaction transaction = transactionDao.queryTransactionById(id);

        boolean flag = false;
        Item item = itemDao.queryItemById(transaction.getItem_id());
        if(state.equals("1")){//同意租借
            //商品下架
            item.setBorrow_state("borrowed");

            //更新借出者和借入者的账户余额
            String lender_id = transaction.getLender_id();
            String borrow_id = transaction.getBorrow_id();
            double amount = transaction.getAmount();
            Account lender_account = accountDao.queryAccountById(lender_id);
            Account borrow_account = accountDao.queryAccountById(borrow_id);
            lender_account.setBalance(lender_account.getBalance()+amount);
            borrow_account.setBalance(borrow_account.getBalance()-amount);

            accountDao.updateAccount(lender_account);
            accountDao.updateAccount(borrow_account);
        }else if(state.equals("2")){//交易完成
            item.setBorrow_state("allow_borrow");
        }
        flag = itemDao.updateItem(item);
        transaction.setState(state);
        return transactionDao.updateTransaction(transaction) && flag;
    }

    /*
     * 删除语句
     * 1.根据订单id删除订单   deleteTransactionById(int id)
     * 
     */

    public boolean deleteTransactionById(int id){
    	return transactionDao.deleteTransactionById(id);
    }
    
    /*
     * 更新语句
     * 1.根据订单编号更新，目前只需要更新交易状态 
     *    -1代表交易待确认，0代表交易进行中，1代表交易完成
     */
    public boolean updateTransaction(Transaction transaction){
    	return transactionDao.updateTransaction(transaction);
    }

    

}
